package com.ggh.jinjilive.ui;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.dialog.LoadDialog;
import com.ggh.jinjilive.dialog.PayChooseDialog;
import com.ggh.jinjilive.mobile.UserMobile;
import com.ggh.jinjilive.remotedata.ViewModelFactory;
import com.kingja.loadsir.core.LoadService;

import java.text.DecimalFormat;

import javax.inject.Inject;

public abstract class BaseActivity<V extends ViewDataBinding> extends AppCompatActivity {
    protected Context mContext;
    //布局
    protected V binding;
    @Inject
    protected ViewModelFactory viewModelFactory;
    //用户信息
    protected UserMobile userMobile;
    //
    protected LoadDialog loadDialog;
    //标题
    protected Toolbar mToolbar;
    //网络问题或者暂无数据
    protected LoadService loadService;
    //支付弹框
    protected PayChooseDialog payChooseDialog;
    //区分是支付宝还是微信
    protected int WeChatAlipay = 0;
    //double四舍五入
    protected DecimalFormat decimalFormat;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        //用户信息
        userMobile = MerchantSession.getInstance().getUser();
        //支付弹框
        payChooseDialog = new PayChooseDialog(mContext);
        //double四舍五入
        decimalFormat = new DecimalFormat("######0.00");
        //
        initCreate();
        //页面接受的参数方法
        initParam();
        //私有的初始化Databinding和ViewModel方法
        initViewDataBinding(savedInstanceState);
        //页面初始化方法
        initView();
        //页面数据初始化方法
        initData();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initViewObservable();

    }

    public void initCreate(){

    }


    public void initParam() {

    }


    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView(Bundle savedInstanceState);

    /**
     * 注入绑定
     */
    private void initViewDataBinding(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, initContentView(savedInstanceState));
    }

    public void initView() {

    }


    public void initData() {

    }

    public void initViewObservable() {

    }


    public void showDialog() {
        if (!isFinishing()) {
            if(null != loadDialog && loadDialog.isShowing()){
                return;
            }
            loadDialog = new LoadDialog(mContext);
            try {
                loadDialog.show();
            }catch (Exception e){
                e.getMessage();
            }
        }
    }


    public void cancelDialog() {
        if (loadDialog != null && !isFinishing()) {
            loadDialog.dismiss();
        }
    }


    /**
     * @param title       标题
     * @param isShowBreak 返回键
     * @param isSwitching 返回件颜色
     */
    public void setWhiteActionBarStyle(String title, boolean isShowBreak, boolean isSwitching) {
        if (mToolbar == null) {
            mToolbar = findViewById(R.id.toolBar);
//            findViewById(R.id.toolBar).setBackgroundColor(Color.WHITE);
            setSupportActionBar(mToolbar);
        }
        if (null != mToolbar) {
            TextView customView = (TextView) getLayoutInflater().inflate(R.layout.layout_text, null);
            customView.setText(title);
            if (isSwitching) {
                customView.setTextColor(Color.WHITE);
            } else {
                customView.setTextColor(Color.BLACK);
            }
            ActionBar.LayoutParams customViewLayoutParams = new ActionBar.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ActionBar.LayoutParams lp = customViewLayoutParams;
            lp.gravity = Gravity.CENTER;
            getSupportActionBar().setCustomView(customView, lp);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            if (isShowBreak) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                if (isSwitching) {
                    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_white_return);
                } else {
                    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_black_return);
                }
            }
        }
    }

    /**
     * 隐藏软键盘
     */
    protected void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        Resources resources = this.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = 1;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }




    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

}
